package com.jzit168.library_base.ext

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.lifecycle.asLiveData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn

/**
 *@Desc:
 *@Author: Android Developer
 *@Date: 2020/10/16
 *
 **/

//将flow转换为LiveData,并切回主线程
fun <T> Flow<T>.asLiveDataUI(
    lifecycleOwner: LifecycleOwner,
    observer: Observer<T>
) {
    asLiveData(Dispatchers.Main).observe(lifecycleOwner, observer)
}

//将flow切换到IO线程
fun <T> Flow<T>.flowIO() = flowOn(Dispatchers.IO)